package com.wbs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wbs.mapper.TbSellerMapper;
import com.wbs.model.TbSeller;
import com.wbs.model.TbSellerExample;
import com.wbs.resutil.PageBean;
import com.wbs.resutil.PageResult;
import com.wbs.resutil.Result;
import com.wbs.resutil.ResultEnums;
import com.wbs.service.SellerService;
import com.wbs.vo.SellerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.wbs.utils.ShiroUtils.encryptPassword;
import static com.wbs.utils.ShiroUtils.generateSalt;

/**
 * @Author: 芝梦
 * @Description:
 * @DateTime: 2024/1/26 20:05
 */
@Service
public class SellerServiceImpl implements SellerService {
    @Autowired
    private TbSellerMapper sellerMapper;


    @Override
    public TbSeller selectSellerBySellerId(String sellerId) {
        return sellerMapper.selectByPrimaryKey(sellerId);
    }

    //注册
    @Override
    public Result register(TbSeller tbSeller) {
        //判断用户是否已存在
        if (this.findItBySellerId(tbSeller.getSellerId()) != null) {
            return Result.RESPONSE(ResultEnums.THE_USERNAME_ALREADY_EXISTS);
        }
        //判断密码或账户是否为空
        if (StrUtil.isEmpty(tbSeller.getSellerId()) && StrUtil.isEmpty(tbSeller.getPassword())) {
            return Result.RESPONSE(ResultEnums.USERNAME_AND_PASSWORD_ERROR);
        }
        //密码加盐
        String salt = generateSalt(8);
        tbSeller.setSalt(salt);
        tbSeller.setPassword(encryptPassword("md5", tbSeller.getPassword(), salt, 7));

        //是否增加失败
        if (sellerMapper.insertSelective(tbSeller) == 0) {
            return Result.FAIL();
        }
        return Result.SUCCESS();
    }

    //按条件分页查询
    @Override
    public PageResult<SellerVo> findByConditionPage(PageBean<SellerVo> pageBean) {
        //设置分页参数
        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        //模糊查询 调用查询条件方法 buildExample
        List<TbSeller> tbSellerList = sellerMapper.selectByExample(buildExample(pageBean.getBean()));
        PageInfo<TbSeller> pageInfo = new PageInfo<>(tbSellerList);
        //集合转换为Vo
        List<SellerVo> sellerList = pageInfo.getList().stream().map(this::convertVo
        ).collect(Collectors.toList());

        return new PageResult<>(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),sellerList);
    }

    @Override
    public void update(String status, String sellerId) {
        TbSeller tbSeller = new TbSeller();
        tbSeller.setSellerId(sellerId);
        tbSeller.setStatus(status);
        sellerMapper.updateByPrimaryKeySelective(tbSeller);
    }


    /*辅助方法---------------------------------------------------------------------------------------------------------------------------------*/
    //通过主键查询
    private TbSeller findItBySellerId(String sellerId) {
        return sellerMapper.selectByPrimaryKey(sellerId);
    }

    //构建条件类
    private TbSellerExample buildExample(SellerVo criteria) {
        TbSellerExample example = new TbSellerExample();
        TbSellerExample.Criteria criteria1 = example.createCriteria();
        //用户ID
        if (!StrUtil.isEmpty(criteria.getSellerId())) {
            criteria1.andSellerIdEqualTo(criteria.getSellerId());
        }
        //公司名称
        if (!StrUtil.isEmpty(criteria.getName())) {
            criteria1.andNameLike("%" + criteria.getName() + "%");
        }
        //店铺名称
        if (!StrUtil.isEmpty(criteria.getNickName())) {
            criteria1.andNickNameLike("%" + criteria.getNickName() + "%");
        }
        //状态
        if (!StrUtil.isEmpty(criteria.getStatus())) {
            criteria1.andStatusEqualTo(criteria.getStatus());
        }
        return example;
    }

    private SellerVo convertVo(TbSeller tbSeller) {
        SellerVo sellerVo = new SellerVo();
        BeanUtils.copyProperties(tbSeller, sellerVo);
        return sellerVo;
    }

}

